import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 准备actions--用于响应组件中的动作
const actions = {
    // 计算
    jia(context, payload) {
        console.log('action jia 被调用 ', context, payload)
        context.commit('JIA', payload)
    },
    jian(context, payload) {
        console.log('action jian 被调用 ', context, payload)
        context.commit('JIAN', payload)
    },
    oodAdd(context, payload) {
        if (payload % 2 !== 0) {
            context.commit('JIA', payload)
        }
    },
    waitAdd(context, payload) {
        setTimeout(() => {
            context.commit('JIA', payload)
        }, 1000)
    },

    // 学生
    add(context, payload) {
        context.commit('ADD', payload)
    },
    addWang(context, payload) {
        if (payload.name.indexOf('王') > -1) {
            context.commit('ADD', payload)
        }
    }
}

// 准备mutations--用于操作数据
const mutations = {
    JIA(state, payload) {
        console.log('mutaion JIA 被调用 ', state, payload)
        state.number += payload
    },
    JIAN(state, payload) {
        console.log('mutaion JIAN 被调用 ', state, payload)
        state.number -= payload
    },

    // 学生
    ADD(state, payload) {
        console.log('mutaion ADD 被调用 ', state, payload)
        state.students.unshift(payload)
    }
}

// 准备state--用于存储数据
const state = {
    number: 0,
    school: '岳麓区第一高级中学',
    subject: '数学',
    students: [{
        id: '001',
        name: '张三',
        age: 18
    },{
        id: '002',
        name: '李四',
        age: 20
    }]
}

const getters = {
    msg(state) {
        console.log('@@',state)
        return 'hello'
    },
    number(state) {
        return state.number
    },
    bigNumber(state) {
        return state.number * 10
    }
}


const countOptions = {
    namespaced: true,
    actions: {
        // 计算
        jia(context, payload) {
            console.log('action jia 被调用 ', context, payload)
            context.commit('JIA', payload)
        },
        jian(context, payload) {
            console.log('action jian 被调用 ', context, payload)
            context.commit('JIAN', payload)
        },
        oodAdd(context, payload) {
            if (payload % 2 !== 0) {
                context.commit('JIA', payload)
            }
        },
        waitAdd(context, payload) {
            setTimeout(() => {
                context.commit('JIA', payload)
            }, 1000)
        },
    },
    mutations: {
        JIA(state, payload) {
            console.log('mutaion JIA 被调用 ', state, payload)
            state.number += payload
        },
        JIAN(state, payload) {
            console.log('mutaion JIAN 被调用 ', state, payload)
            state.number -= payload
        },
    },
    state: {
        number: 0,
        school: '岳麓区第一高级中学',
        subject: '数学',
    },
    getters: {
        msg(state) {
            console.log('@@',state)
            return 'hello'
        },
        number(state) {
            return state.number
        },
        bigNumber(state) {
            return state.number * 10
        }
    }
}

const studentOptions = {
    namespaced: true,
    actions: {
        // 学生
        add(context, payload) {
            context.commit('ADD', payload)
        },
        addWang(context, payload) {
            if (payload.name.indexOf('王') > -1) {
                context.commit('ADD', payload)
            }
        }
    },
    mutations: {
        // 学生
        ADD(state, payload) {
            console.log('mutaion ADD 被调用 ', state, payload)
            state.students.unshift(payload)
        }
    },
    state: {
        students: [{
            id: '001',
            name: '张三',
            age: 18
        },{
            id: '002',
            name: '李四',
            age: 20
        }]
    },
    getters: {

    }
}

export default new Vuex.Store({
    // actions,
    // mutations,
    // state,
    // getters,
    modules: {
        countAbout: countOptions,
        studentAbout: studentOptions
    }
})
